package edu.neu.chenhan.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import edu.neu.chenhan.R;
import edu.neu.chenhan.fragment.cooperation.CooperationHomeFragment;
import edu.neu.chenhan.fragment.news.NewsListFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView imgCooperation, imgIndustryNews;
    private TextView txtCooperation, txtIndustryNews;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_with_bottom_nav);

        // 初始化底部导航视图
        initBottomNavigation();

        // 默认显示合作界面
        switchFragment(new CooperationHomeFragment());
        setSelectedTab(0);
    }

    private void initBottomNavigation() {
        // 获取导航项视图
        View navCooperation = findViewById(R.id.nav_cooperation);
        View navIndustryNews = findViewById(R.id.nav_industry_news);

        imgCooperation = findViewById(R.id.img_cooperation);
        imgIndustryNews = findViewById(R.id.img_industry_news);
        txtCooperation = findViewById(R.id.txt_cooperation);
        txtIndustryNews = findViewById(R.id.txt_industry_news);

        // 检查视图是否成功初始化
        if (navCooperation == null || navIndustryNews == null ||
                imgCooperation == null || imgIndustryNews == null ||
                txtCooperation == null || txtIndustryNews == null) {
            throw new IllegalStateException("Navigation views not found in layout");
        }

        // 设置点击事件
        navCooperation.setOnClickListener(this);
        navIndustryNews.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.nav_cooperation) {
            switchFragment(new CooperationHomeFragment());
            setSelectedTab(0);
        } else if (id == R.id.nav_industry_news) {
            switchFragment(new NewsListFragment());
            setSelectedTab(1);
        }
    }

    private void switchFragment(Fragment fragment) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, fragment)
                .commit();
    }

    private void setSelectedTab(int position) {
        // 重置所有导航项状态
        imgCooperation.setColorFilter(getResources().getColor(R.color.nav_text_color));
        imgIndustryNews.setColorFilter(getResources().getColor(R.color.nav_text_color));
        txtCooperation.setTextColor(getResources().getColor(R.color.nav_text_color));
        txtIndustryNews.setTextColor(getResources().getColor(R.color.nav_text_color));

        // 设置选中项状态
        switch (position) {
            case 0:
                imgCooperation.setColorFilter(getResources().getColor(R.color.nav_text_color_selected));
                txtCooperation.setTextColor(getResources().getColor(R.color.nav_text_color_selected));
                break;
            case 1:
                imgIndustryNews.setColorFilter(getResources().getColor(R.color.nav_text_color_selected));
                txtIndustryNews.setTextColor(getResources().getColor(R.color.nav_text_color_selected));
                break;
        }
    }
}